package com.example.ws.handler.elog;

import com.example.ws.util.Tuple3;

public class ELogUtils {

    // 是否存在role，提取后infoStr，roleStr
    public static Tuple3<Boolean, String, String> extractRole(String infoStr) {
        //       has  infoStr tag_replace
        Tuple3<Boolean,String,String> tag = extractTag("{role, ", "{slot_role_record}", infoStr);
        return tag;
    }

    public static Tuple3<Boolean, String, String> extractFighter(String infoStr) {
        return extractTag("{fighter, ", "{slot_fighter_record}", infoStr);
    }

    public static Tuple3<Boolean, String, String> extractTag(String tag, String replaceTag, String infoStr) {
        int startIndex = infoStr.indexOf(tag);
        int caseLeftCount = 1;
        int endIndex = 0;
        if (startIndex < 0) {
            return new Tuple3<>(false, infoStr, "");
        }
        for (int i = startIndex + 6; i < infoStr.length(); i++) {
            if (infoStr.charAt(i) == '}') {
                caseLeftCount--;
            } else if (infoStr.charAt(i) == '{') {
                caseLeftCount++;
            }
            if (caseLeftCount == 0) {
                endIndex = i;
                String roleTag = replaceTag;
                String newInfoStr = infoStr.substring(0, startIndex) + roleTag + infoStr.substring(endIndex + 1, infoStr.length());
                if (endIndex + 1 - startIndex < 1000) {
                    extractRole(infoStr.substring(endIndex));
                } else {
                    return new Tuple3<>(true, newInfoStr, infoStr.substring(startIndex, endIndex + 1));
                }
            }

        }
        return new Tuple3<>(false, infoStr, "");
    }

    public static void main(String[] args) {
        String s = "[ERROR][2024/4/26 16:15:5][role_handle:432] [xll8]的角色进程处理命令[9900][{p_9900_c2s,9900,<<114,109,102,97,32,91,99,97,109,112,95,114,101,97,108,95,114,101,100,98,97,103,95,109,103,114,44,108,111,97,100,95,103,114,97,110,116,95,100,97,116,97,93>>}]时出错, 原因:undef, 调用栈:[{camp_real_redbag_mgr,load_grant_data,[{role,69,<0.28913.0>,19,false,false,{1714118801,1692804964},1688481171,{192,168,68,189},{192,168,68,189},0,{12,<<120,108,108,108,111,99,97,108>>,2},<<120,108,108,108,111,99,97,108>>,2,<<56,56,56>>,<<54,54,54>>,<<66,55,54,48,77,32,68,83,51,72,32,68,68,82,52,32,40,71,105,103,97,98,121,116,101,32,84,101,99,104,110,111,108,111,103,121,32,67,111,46,44,32,76,116,100,46,41>>,<<49,48,48,48,48>>,<<49,48,48,48,48>>,<<120,108,108,56>>,<<120,108,108,49>>,0,0,<<120,108,108,56>>,0,2,60,60,130,8368,4026,8368,4026,{assets,1,26665,20010000,50005400,20000000,#{90006=>240,90010=>1100,90060=>284,90079=>220}},";
        System.out.println(s.indexOf("{role,"));
        System.out.println(s.charAt(6));
        System.out.println(s.substring(1, 3));
    }
}
